package com.yanqu.road.dao.impl.unioncampwar;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.unioncampwar.UnionCampWarCampData;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UnionCampWarCampDaoImpl extends TempDataObjectDao<UnionCampWarCampData> {

    public static String getUnionCampWarCampDataSql = "select * from `t_c_union_camp_war_camp` where `weekId`=?";

    public Map<Integer, Map<Integer, UnionCampWarCampData>> getUnionCampWarCampDataMap(int weekId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, Map<Integer, UnionCampWarCampData>> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getUnionCampWarCampDataSql);
            int i = 1;
            call.setInt(i++, weekId);
            rs = call.executeQuery();
            while (rs.next()) {
                UnionCampWarCampData data = parse(rs);
                Map<Integer, UnionCampWarCampData> centerGroupMap = dataMap.get(data.getCenterGroupId());
                if (centerGroupMap == null) {
                    centerGroupMap = new ConcurrentHashMap<>();
                    dataMap.put(data.getCenterGroupId(), centerGroupMap);
                }
                centerGroupMap.put(data.getGroupId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateUnionCampWarCampDataSql = "update `t_c_union_camp_war_camp` set endTime=?,winCampId=?,autoSend=?,leftCampHp=?,rightCampHp=?,leftBoxRound=?,leftBigReward=?,leftBigBoxId=?,leftOpenBoxInfoMap=?,rightBoxRound=?,rightBigReward=?,rightBigBoxId=?,rightOpenBoxInfoMap=?,worshipList=?,routeListMap=?,avgAbilityList=?,modifyTime=? where centerGroupId=? and groupId=? and weekId=?";
    @Override
    protected boolean updateData(UnionCampWarCampData unionCampWarCampData) {
        unionCampWarCampData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, unionCampWarCampData.getEndTime()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarCampData.getWinCampId()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarCampData.getAutoSend()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarCampData.getLeftCampHp()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarCampData.getRightCampHp()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarCampData.getLeftBoxRound()));
        para.put(i++, new DbParameter(Types.VARCHAR, unionCampWarCampData.getLeftBigReward()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarCampData.getLeftBigBoxId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(unionCampWarCampData.getLeftOpenBoxInfoMap())));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarCampData.getRightBoxRound()));
        para.put(i++, new DbParameter(Types.VARCHAR, unionCampWarCampData.getRightBigReward()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarCampData.getRightBigBoxId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(unionCampWarCampData.getRightOpenBoxInfoMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(unionCampWarCampData.getWorshipList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(unionCampWarCampData.getRouteListMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(unionCampWarCampData.getAvgAbilityList())));
        para.put(i++, new DbParameter(Types.BIGINT, unionCampWarCampData.getModifyTime()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarCampData.getCenterGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarCampData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarCampData.getWeekId()));
        if (getDb().executeNoneQuery(updateUnionCampWarCampDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUnionCampWarCampDataSql = "insert into `t_c_union_camp_war_camp`(centerGroupId,groupId,weekId,endTime,winCampId,autoSend,leftCampHp,rightCampHp,leftBoxRound,leftBigReward,leftBigBoxId,leftOpenBoxInfoMap,rightBoxRound,rightBigReward,rightBigBoxId,rightOpenBoxInfoMap,worshipList,routeListMap,avgAbilityList,modifyTime) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(UnionCampWarCampData unionCampWarCampData) {
        unionCampWarCampData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarCampData.getCenterGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarCampData.getGroupId()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarCampData.getWeekId()));
        para.put(i++, new DbParameter(Types.BIGINT, unionCampWarCampData.getEndTime()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarCampData.getWinCampId()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarCampData.getAutoSend()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarCampData.getLeftCampHp()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarCampData.getRightCampHp()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarCampData.getLeftBoxRound()));
        para.put(i++, new DbParameter(Types.VARCHAR, unionCampWarCampData.getLeftBigReward()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarCampData.getLeftBigBoxId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(unionCampWarCampData.getLeftOpenBoxInfoMap())));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarCampData.getRightBoxRound()));
        para.put(i++, new DbParameter(Types.VARCHAR, unionCampWarCampData.getRightBigReward()));
        para.put(i++, new DbParameter(Types.INTEGER, unionCampWarCampData.getRightBigBoxId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(unionCampWarCampData.getRightOpenBoxInfoMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(unionCampWarCampData.getWorshipList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(unionCampWarCampData.getRouteListMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(unionCampWarCampData.getAvgAbilityList())));
        para.put(i++, new DbParameter(Types.BIGINT, unionCampWarCampData.getModifyTime()));
        if (getDb().executeNoneQuery(insertUnionCampWarCampDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UnionCampWarCampData parseData(ResultSet rs) throws SQLException {
        UnionCampWarCampData unionCampWarCampData = new UnionCampWarCampData();
        unionCampWarCampData.setCenterGroupId(rs.getInt("centerGroupId"));
        unionCampWarCampData.setGroupId(rs.getInt("groupId"));
        unionCampWarCampData.setWeekId(rs.getInt("weekId"));
        unionCampWarCampData.setEndTime(rs.getLong("endTime"));
        unionCampWarCampData.setWinCampId(rs.getInt("winCampId"));
        unionCampWarCampData.setAutoSend(rs.getInt("autoSend"));
        unionCampWarCampData.setLeftCampHp(rs.getInt("leftCampHp"));
        unionCampWarCampData.setRightCampHp(rs.getInt("rightCampHp"));
        unionCampWarCampData.setLeftBoxRound(rs.getInt("leftBoxRound"));
        unionCampWarCampData.setLeftBigReward(rs.getString("leftBigReward"));
        unionCampWarCampData.setLeftBigBoxId(rs.getInt("leftBigBoxId"));
        unionCampWarCampData.setLeftOpenBoxInfoMap(JSON.parseObject(rs.getString("leftOpenBoxInfoMap"), new TypeReference<ConcurrentHashMap<java.lang.Integer, com.yanqu.road.entity.unioncampwar.UnionCampWarOpenBoxInfo>>() {}));
        unionCampWarCampData.setRightBoxRound(rs.getInt("rightBoxRound"));
        unionCampWarCampData.setRightBigReward(rs.getString("rightBigReward"));
        unionCampWarCampData.setRightBigBoxId(rs.getInt("rightBigBoxId"));
        unionCampWarCampData.setRightOpenBoxInfoMap(JSON.parseObject(rs.getString("rightOpenBoxInfoMap"), new TypeReference<ConcurrentHashMap<java.lang.Integer, com.yanqu.road.entity.unioncampwar.UnionCampWarOpenBoxInfo>>() {}));
        String worshipList = rs.getString("worshipList");
        if (!StringUtils.isNullOrEmpty(worshipList)) {
            unionCampWarCampData.setWorshipList(JSON.parseObject(worshipList, new TypeReference<java.util.List<Integer>>() {
            }));
        }
        String routeListMap = rs.getString("routeListMap");
        if (!StringUtils.isNullOrEmpty(routeListMap)) {
            unionCampWarCampData.setRouteListMap(JSON.parseObject(routeListMap, new TypeReference<java.util.Map<Integer, java.util.List<Integer>>>() {
            }));
        }

        unionCampWarCampData.setAvgAbilityList(JSON.parseObject(rs.getString("avgAbilityList"), new TypeReference<java.util.List<java.math.BigDecimal>>() {}));
        unionCampWarCampData.setModifyTime(rs.getLong("modifyTime"));

        return unionCampWarCampData;
    }
}
